﻿namespace Mocks
{
    public enum State
    {
        DoorClosed,
        DoorOpening,
        DoorOpen,
        DoorBreached
    }

    public class DoorControl
    {
        public IDoor Door { get; set; }
        public ICardDB CardDB { get; set; }
        public IBeeper Beeper { get; set; }
        public IAlarm Alarm { get; set; }

        public State CurrentState { get; set; }

        public DoorControl(IDoor door, ICardDB cardDB, IBeeper beeper, IAlarm alarm)
        {
            CurrentState = State.DoorClosed;
            Door = door;
            CardDB = cardDB;
            Beeper = beeper;
            Alarm = alarm;
        }

        public void RequestEntry(int id)
        {
            switch (CurrentState)
            {
                case State.DoorClosed:
                    if (CardDB.ValidateEntryRequest(id))
                    {
                        Door.Open();
                        Beeper.MakeHappyNoise();
                        CurrentState = State.DoorOpening;
                    }
                    else
                    {
                        Beeper.MakeUnhappyNoise();
                    }
                    break;
            }
        }

        public void DoorOpened()
        {
            switch (CurrentState)
            {
                case State.DoorOpening:
                    CurrentState = State.DoorOpen;
                    Door.Close();
                    break;
                case State.DoorClosed:
                    Door.Close();
                    Alarm.SoundAlarm();
                    CurrentState = State.DoorBreached;
                    break;
            }
        }

        public void DoorClosed()
        {
            switch (CurrentState)
            {
                case State.DoorOpen:
                    CurrentState = State.DoorClosed;
                    break;
            }
        }
    }
}
